package com.yihuier.yihuier.Service.MeetingManager;

import com.yihuier.yihuier.Entity.MeetingManager.DTO.UserAttendenceDTO;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingApplyMapper;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingAttendMapper;
import com.yihuier.yihuier.Mapper.UserManager.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class MeetingAttendService {
    @Autowired
    private MeetingAttendMapper meetingAttendMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 得到我参会的状态信息
     */
    public String getAttend_attendence(int user_id, Long meeting_id) {
        return this.meetingAttendMapper.selectAttend_attendence(user_id, meeting_id);
    }

public boolean updateUserAttendReson(int user_id,int meeting_id,String reason){
        return this.meetingAttendMapper.updateOneRecordOfMeetingUserWithReason(user_id,meeting_id,2,reason) > 0;    }

    /**
     * 会议结束后将未到会的人的状态置为缺勤
     */
    public int updateAttendence(int meeting_id) {
        return meetingAttendMapper.updateAttendence(meeting_id);
    }

    public List<UserAttendenceDTO> getUserMeetingStatistics(int[] user_ids) {
        List<UserAttendenceDTO> userAttendenceDTOS = new LinkedList<>();

        for (int user_id : user_ids){
            UserAttendenceDTO userAttendenceDTO = new UserAttendenceDTO();
            userAttendenceDTO.setUser_id(user_id);
            String username = userMapper.selectUserNameByUserId(String.valueOf(user_id));
            if (username == null || username.equals("")){
                continue;
            }
            userAttendenceDTO.setUsername(username);
            int total = meetingAttendMapper.selectNumUserTotalAttendence(user_id);
            int ontime = meetingAttendMapper.selectNumUserStatusAttendence(user_id,0);
            int late = meetingAttendMapper.selectNumUserStatusAttendence(user_id,1);
            int ask_for_leave = meetingAttendMapper.selectNumUserStatusAttendence(user_id,2);
            int absence = meetingAttendMapper.selectNumUserStatusAttendence(user_id,3);
            int not_arrived = meetingAttendMapper.selectNumUserStatusAttendence(user_id,4);

            userAttendenceDTO.setTotal(total);
            userAttendenceDTO.setOntime(ontime);
            userAttendenceDTO.setLate(late);
            userAttendenceDTO.setAsk_for_leave(ask_for_leave);
            userAttendenceDTO.setAbsence(absence);
            userAttendenceDTO.setNot_arrived(not_arrived);

            userAttendenceDTOS.add(userAttendenceDTO);
        }
        if (userAttendenceDTOS.size() == 0){
            return null;
        }else {
            return userAttendenceDTOS;
        }
    }

    public int addOneRecordOfMeetingUserWithOutReason(int user_id,int meeting_id){
        return meetingAttendMapper.addOneRecordOfMeetingUserWithOutReason(user_id,meeting_id);
    }
}
